<template>
	<view class="container">
		<image src="/static/logo2.png" class="logo"></image>
		<view class="title">欢迎来到恋爱交友小程序</view>
		<view class="subtitle">寻找心灵契合的另一半</view>

		<button class="login-btn" @click="MyLogin()">微信登录</button>
	</view>
</template>

<script>
	import config from '@/common/config.js'
	export default {
		data() {
			return {
				gender: '',
				nickName: '派大星',
				HeaderUrl: '../../static/logo.png',
				openid: '111',
				userInfo: {
					avataUrl: '',
					nickName: '',
					openid: ''
				}
			}
		},
		methods: {

			MyLogin() {
				this.wxlogin()
				console.log(this.userInfo);
			},

			wxlogin() {
				let that = this
				console.log("login");
				this.getUserOpenid()
				uni.getUserProfile({
					desc: "获取你的昵称、 头像 ",
					success: (res) => {
						if (res.errMsg == 'getUserProfile:ok' && res.userInfo != 'undefined') {
							// console.log(res);
							this.userInfo.nickName = res.userInfo.nickName
							this.userInfo.HeaderUrl = res.userInfo.avatarUrl
							console.log(that.userInfo);
							var pid = that.userInfo.openid
							that.$global.personInfo.openid = that.userInfo.openid
							console.log(pid);
							uni.request({
								url: config.baseUrl + 'user/register',
								method: "POST",
								data: {
									"openid": pid
								},
								success: (res) => {
									// console.log(getApp().globalData.openid);
									if (res.statusCode == 200) {
										uni.switchTab({
											url: '/pages/Home/Home'
										})
									} else {
										uni.showToast({
											title: '错误',
											icon: 'error'
										})
									}

								},
								fail: (err) => {
									console.log(err)
								}
							})
						}
					},
					fail: () => {}
				})
			},
			getUserOpenid() {
				console.log('登录');
				let that = this
				uni.login({
					provider: "weixin",
					success: (res) => {
						// console.log(res);
						uni.request({
							url: config.baseUrl + 'user/getUserOpenid',
							method: 'POST',
							data: {
								code: res.code
							},
							success: (res) => {
								// console.log(res.data.openid);
								that.userInfo.openid = res.data.openid
								// console.log(userInfo);
								return res.data.openid
							}
						})
					}
				})
			}
		}
	}
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
		background: linear-gradient(to right, #87CEEB, #FF69B4);
	}

	.logo {
		width: 120px;
		height: 120px;
		margin-bottom: 20px;
	}

	.gender {
		display: flex;
		align-items: center;
	}

	.radio {
		display: flex;
		flex-direction: row;
	}

	.title {
		font-size: 24px;
		font-weight: bold;
		margin-bottom: 10px;
		color: #fff;
	}

	.subtitle {
		font-size: 16px;
		color: #fff;
		/* margin-bottom: 30px; */
	}

	.login-btn {
		padding: 10px 20px;
		background-color: #fff;
		color: #ff4d64;
		border-radius: 5px;
		font-size: 16px;
		cursor: pointer;
	}
</style>